package com.xgr.wonderful.activity;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.markupartist.android.widget.ActionBar;
import com.markupartist.android.widget.ActionBar.Action;
import com.xgr.wonderful.R;
import com.xgr.wonderful.activity.base.BasePageActivity;
import com.xgr.wonderful.adapter.StatusContentAdapter;
import com.xgr.wonderful.constant.Constant;
import com.xgr.wonderful.entity.Status;
import com.xgr.wonderful.utils.Cache;
import com.xgr.wonderful.utils.HttpProxy;
import com.xgr.wonderful.utils.HttpProxy.IHttpRequestListener;
import com.xgr.wonderful.utils.LogUtils;

/**
 * 显示我收到 的评论的状态
 * @author lsj
 *
 */
public class CommentReceivedActivity extends BasePageActivity{
	
	private ActionBar mActionBar ;
	
	private List<Status> mStatusList = new ArrayList<Status>() ;
	private StatusContentAdapter mStatusAdapter ;
	private int clickIndex = -1 ;
	
	private PullToRefreshListView mPullRefreshListView;
	private ListView actualListView;
	private TextView commentReceivedTips ;
	
	private HttpProxy httpProxy ;
	private Status queryStatus ;
	private List<Status>  tempStatusList = new ArrayList<Status>() ;
	
	@Override
	protected void setLayoutView() {
		setContentView(R.layout.activity_comment_received) ;
	}

	@Override
	protected void findViews() {
		mActionBar = (ActionBar)findViewById(R.id.actionbar_comment_received) ;
		mPullRefreshListView = (PullToRefreshListView)findViewById(R.id.comment_received_pull_list) ;
		commentReceivedTips = (TextView)findViewById(R.id.comment_received_tips) ;
	}

	@Override
	protected void setupViews(Bundle bundle) {
		mActionBar.setTitle("收到评论的状态") ;
		mActionBar.setDisplayHomeAsUpEnabled(true) ;
		mActionBar.setHomeAction(new Action() {
			@Override
			public void performAction(View view) {
				//相当于是清除记录
//				new Thread(){
//					@Override
//					public void run() {
//						super.run();
//						Cache.putCommentReceivedStatus(null) ;
//					}
//					
//				}.start();
				finish() ; 
			}
			@Override
			public int getDrawable() {
				return R.drawable.weix_logo_blank;
			}
		}) ;
		//取消新评论的提醒，要不然会有null问题
		notificationManager.cancel(Constant.NEW_COMMENT_NOTIFICATION_ID) ;
		httpProxy = new HttpProxy();
		//得到收到评论的状态 
		tempStatusList = Cache.getCommentRecevicedStatus() ;
		if (tempStatusList == null || tempStatusList.size()==0){
			commentReceivedTips.setText("没有新的评论哦,试着去发表新的状态吧") ;
		}else {
			LogUtils.i(TAG, "tempStatusList:"+ new Gson().toJson(tempStatusList)) ;
			for (Status tempStatus : tempStatusList){
				fetchData(tempStatus);
			}
		}
		//这里不可以拉
		mPullRefreshListView.setMode(Mode.DISABLED) ;
		actualListView = mPullRefreshListView.getRefreshableView() ;
		mStatusAdapter = new StatusContentAdapter(mContext, mStatusList) ;
		actualListView.setAdapter(mStatusAdapter) ;
		// 进入评论页面
		actualListView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				clickIndex = position - 1;
				Intent intent = new Intent();
				intent.setClass(mContext, CommentActivity.class);
				intent.putExtra("data", mStatusList.get(clickIndex));
				startActivityForResult(intent,
						Constant.REQUEST_CODE_COMMENT_NUM);
			}
		});
	}
	/**
	 * 加载全部完成后刷新
	 */
	private void refresh (){
		if (tempStatusList!= null&& mStatusList!= null&&
				tempStatusList.size()== mStatusList.size()){
			mStatusAdapter.notifyDataSetChanged() ;
			mPullRefreshListView.onRefreshComplete() ;
			Cache.putCommentReceivedStatus(null) ;
		}
	
	}
	@Override
	protected void setListener() {
		
	}
	
	private void fetchData (Status tempStatus){
		String statusid = tempStatus.getId().toString() ;
		httpProxy.queryStatusByID(statusid, new IHttpRequestListener() {
			
			@Override
			public void OnHttpRequestSuccessful(String result, int errCode) {
				if (errCode == Constant.ERR_HTTP_REQUEST_SUCCESS){
					queryStatus = new Gson().fromJson(result, Status.class);
					if (queryStatus!= null && queryStatus.getContent()!= null){
						mStatusList.add(queryStatus) ;
						LogUtils.i(TAG, "size");
						refresh() ;
					}
				}else if (errCode == Constant.ERR_HTTP_REQUEST_FAILURE) {
					
				}
			}
			
			@Override
			public void OnHttpRequestStart() {
				
			}
			
			@Override
			public void OnHttpRequestFailure(String info) {
				
			}
		}) ;
	}
	
	

}
